package com.ltwc.lttv.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import com.ltwc.lttv.activity.MainActivity
import com.ltwc.lttv.databinding.DialogBinding


class SettingFragment(
    private val versionName: String,
    private val channelReversal: Boolean,
    private val channelNum: Boolean,
    private val hardDecoding: Boolean,
    private val channelFull: Boolean,
) :
    DialogFragment() {

    private var _binding: DialogBinding? = null
    private val binding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, 0)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = DialogBinding.inflate(inflater, container, false)
        _binding?.version?.text =
            "当前版本: $versionName"

        val switchChannelFull = _binding?.switchChannelFull
        switchChannelFull?.isChecked = channelFull
        switchChannelFull?.setOnCheckedChangeListener { _, isChecked ->
            (activity as MainActivity).saveChannelFull(isChecked)
        }


        val switchChannelReversal = _binding?.switchChannelReversal
        switchChannelReversal?.isChecked = channelReversal
        switchChannelReversal?.setOnCheckedChangeListener { _, isChecked ->
            (activity as MainActivity).saveChannelReversal(isChecked)
        }

        val switchChannelNum = _binding?.switchChannelNum
        switchChannelNum?.isChecked = channelNum
        switchChannelNum?.setOnCheckedChangeListener { _, isChecked ->
            (activity as MainActivity).saveChannelNum(isChecked)
        }
        val switchHardDecoding = _binding?.switchHardDecoding
        switchHardDecoding?.isChecked = hardDecoding
        switchHardDecoding?.setOnCheckedChangeListener { _, isChecked ->
            (activity as MainActivity).saveHardDecoding(isChecked)
        }

        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun onResume() {
        super.onResume()
        val params: ViewGroup.LayoutParams = dialog?.window!!.attributes
        params.width = ViewGroup.LayoutParams.MATCH_PARENT
        params.height = ViewGroup.LayoutParams.MATCH_PARENT
        dialog!!.window!!.attributes = params as WindowManager.LayoutParams
    }

    companion object {
        const val TAG = "SettingFragment"
    }
}

